



#import "XBSaveDataTool.h"

@implementation XBSaveDataTool

/**
 *  保存布尔值
 */
+(void)saveBool:(BOOL)b forKey:(NSString *)key{
    // 保存数据到偏好设置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:b forKey:key];
    [defaults synchronize];
}
+(BOOL)boolForKey:(NSString *)key{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:key];
}


/**
 *  保存字符串
 */
+(void)saveValue:(NSString *)value forKey:(NSString *)key{
    if (key.length == 0) return;
    // 保存数据到偏好设置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:value forKey:key];
    [defaults synchronize];
}
+(NSString *)valueForKey:(NSString *)key{
    if (key.length == 0) return nil;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:key];
}

/**
 *  保存对象
 */
+ (void)saveObject:(id)obj forKey:(NSString *)key{
    if (key.length == 0) return;
    // 保存数据到偏好设置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:obj forKey:key];
    [defaults synchronize];
}
+ (id)objectForKey:(NSString *)key{
    if (key.length == 0) return nil;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:key];
}

/**
 *  NSInteger
 */
+ (void)saveInteger:(NSInteger)integer forKey:(NSString *)key{
    if (key.length == 0) return;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:integer forKey:key];
    [defaults synchronize];
}
+ (NSInteger)integerForKey:(NSString *)key{
    if (key.length == 0) return 0;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:key];
}

//偏好设置一般存储的方法，如果需要存储可以根据这个继续扩展类
-(void)demo
{
    NSUserDefaults *defaluts = [NSUserDefaults standardUserDefaults];
    //设置数据
    [defaluts setObject:@"jack" forKey:@"name"];
    [defaluts setBool:YES forKey:@"sex"];
    [defaluts setInteger:100 forKey:@"age"];
    [defaluts setDouble:1.40 forKey:@"height"];
    [defaluts setFloat:1.9 forKey:@"weight"];
}

//-(void)readDemo
//{
//    NSUserDefaults *defaults = [[NSUserDefaults alloc]init];
//    NSString *name = [defaults objectForKey:@"name"];
//    BOOL sex = [defaults boolForKey:@"sex"];
//    double heigth = [defaults doubleForKey:@"height"];
//    float weight = [defaults floatForKey:@"weight"];
//}


@end
